<template src="./template.html"></template>

<style src="./style.css" scoped></style>
<script>
    import borderBg from 'assets/images/qrcode-border.png';
    import api from "api/api";
    import modal from "utils/modal";
    import navigator from "utils/navigator";
    import notification from "utils/notification";
    import constant from "constants/constant";

    export default{
        data(){
            return {
                pageLoaded: false,
                qrcodeBorderBg: borderBg,
                qrCodeList: [],
                cashMoney: 0,
                oid: null,
                payId: null,
                payDescription: null,
                timer: null,
                time: 3000 //ms
            }
        },
        created() {
            let config = weex.config;

            if(config.oid) this.oid = config.oid; //待付款订单oid

            if(typeof config.payId === 'undefined'){

                let bundleUrl = config.bundleUrl;
                let options = bundleUrl.split('options=')[1];
                options = JSON.parse(options);

                if(typeof options.payId === 'undefined') {
                    modal.toast('payId参数为空, 无法获取交易信息!');
                    return false;
                }

                config = options; //转换
            }

            this.payId = config.payId;
            this.getRemote();
        },
        methods: {
            getRemote(){
                let self = this;
                api.sendHttp("getQrcodeApi",{payid: this.payId}).then(res=>{
                    self.pageLoaded = true;
                    self.vendor(res.data || {});
                },res=>{
                    self.pageLoaded = true;
                    modal.toast(res.msg);
                });
            },
            vendor(data){
                data = data || {};
                if(typeof data.qrcode != 'undefined' && data.qrcode != '') this.qrCodeList = data.qrcode || [];

                if(typeof data.cashMoney) this.cashMoney = data.pay_money;

                if(typeof data.pay_describe != 'undefined') this.payDescription = data.pay_describe || null;

                this.start();//启动监听支付成功接口
            },
            start(){
                if(!this.timer) this.timer = setInterval(this.getPayInfo.bind(this), this.time);
            },
            getPayInfo(){
                let self = this;
                //轮循请求支付成功接口，判断是否支付
                api.sendHttp("getPayQueryApi",{payid: this.payId,timeout: this.time},false).then(res => {
                    self.stop();

                    //发送待付款订单支付成功通知
                    if(self.oid) {
                        notification.send(constant.EVENT_ORDER_PAY_SUCCESS, self.oid);
                    }

                    navigator.pop();
                    let data = res.data || {};
                    let _params = {
                         payId: this.payId,
                         tradenum: data.tradenum || 0,
                         transactioninfo: data.transactioninfo || ''
                    };
                    let options = {
                        classType: constant.PaySuccess_Class_Type,
                        title: '收银台',
                        params: _params
                    };
                    navigator.push(options);
                }, res => {
                    console.log(res.msg)
                });
            },
            stop() {
                clearInterval(this.timer);
                this.timer = null;
            }
        },
        destroyed() {
            this.stop();//组件销毁
        }
    }
</script>
